package com.example.bomc.controller;


import com.example.bomc.common.msg.CodeMsg;
import com.example.bomc.model.Query;
import com.example.bomc.service.queryService.QueryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;


@Controller
public class QueryController {

    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    QueryService queryService;

    @RequestMapping(path = {"/addQuery/"}, method = {RequestMethod.POST})
    @ResponseBody
    public CodeMsg reg(@RequestParam("choose") String choose,
                       @RequestParam("trouble") String trouble,
                       @RequestParam("user") String user,
                       HttpServletResponse response) {

        CodeMsg msg = new CodeMsg();
        try{
            Query query = new Query();
            query.setChoose(choose);
            query.setTrouble(trouble);
            query.setUser(user);
            msg = queryService.addQuery(query);
        }catch (Exception e){
            msg.setRetCode(-1);
            msg.setMessage("提交问题失败！！！");
            logger.error("提交问题失败！！！"+e);
            return msg;
        }
        return msg;
    }


    @RequestMapping(path = {"/selQuery/"}, method = {RequestMethod.GET})
    @ResponseBody
    public CodeMsg selQuery(
                       @RequestParam("trouble") String trouble,

                       HttpServletResponse response) {

        CodeMsg msg = new CodeMsg();
        try{
            msg = queryService.selQuery(trouble);
        }catch (Exception e){
            msg.setRetCode(-1);
            msg.setMessage("查询问题失败！！！");
            logger.error("查询问题失败！！！"+e);
            return msg;
        }
        return msg;
    }


    @RequestMapping(path = {"/selHistory/"}, method = {RequestMethod.GET})
    @ResponseBody
    public CodeMsg selHistory(
                       @RequestParam("user") String user,
                       HttpServletResponse response) {

        CodeMsg msg = new CodeMsg();
        try{
            msg = queryService.selHistory(user);
        }catch (Exception e){
            msg.setRetCode(-1);
            msg.setMessage("提交历史记录失败！！！");
            logger.error("提交历史记录失败！！！"+e);
            return msg;
        }
        return msg;
    }


    @RequestMapping(path = {"/addassess/"}, method = {RequestMethod.POST})
    @ResponseBody
    public CodeMsg addassess(@RequestParam("assess") String assess,
                       @RequestParam("trouble") String trouble,
                       @RequestParam("user") String user,
                       HttpServletResponse response) {

        CodeMsg msg = new CodeMsg();
        try{
            msg = queryService.addassess(trouble, user, assess);
        }catch (Exception e){
            msg.setRetCode(-1);
            msg.setMessage("反馈失败！！！");
            logger.error("反馈失败！！！"+e);
            return msg;
        }
        return msg;
    }
}
